/**
* 系统管理 用户管理
*/
<template>


  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>指标提取</el-breadcrumb-item>
    </el-breadcrumb>

    <div style="padding:5px;color:#000000;text-align: center;font-family:PingFang SC;font-size:30px">指标集配置</div>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="">
        <el-input size="small" @keyup.enter="getSearchPage"  v-model="formInline.fileName" placeholder="输入文件名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="getSearchPage">搜索</el-button>
        <el-button size="small" type="info" icon="el-icon-refresh" @click="reset">重置</el-button>
        <el-button size="small" type="success" icon="el-icon-plus" @click="handleEdit()">新增任务</el-button>
        <!--        <el-button size="small" type="primary" @click="handleunit()">部门设置</el-button>-->
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" @selection-change="selectChange" :data="taskData" highlight-current-row v-loading="loading" border element-loading-text="执行任务中" style="width: 100%;">
      <el-table-column v-if="false" align="center" type="selection" width="40">
      </el-table-column>
      <el-table-column align="center" sortable prop="id" v-if="false" label="序号" width="80">
      </el-table-column>
      <el-table-column align="center" sortable prop="filevo.fileName" label="文件" width="350">
      </el-table-column>

      <el-table-column align="center" prop="indicatorId" label="指标" width="200">
      </el-table-column>
      <el-table-column align="center" sortable prop="status" label="状态" min-width="60">
      </el-table-column>

      <!-- 操作 -->
      <el-table-column align="center" label="操作" min-width="300">
        <template slot-scope="scope">
          <el-button size="mini" type="primary"@click="actionNow(scope.row.id,scope.row.status)">立即执行</el-button>
          <el-button size="mini" type="danger" @click="deleteTask(scope.row.id)">删除</el-button>
          <el-button size="mini" type="success" @click="getRes(scope.row.id,scope.row.status,scope.row.filevo.fileName)">查看结果</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible"  width="70%" @click='closeDialog("edit")'>
      <el-form label-width="100px" ref="editForm" :model="editForm">

        <el-form-item label="指标" prop="name">
          <el-select size="mini" v-model="indicatorIdArray" multiple placeholder="请选择指标">
            <el-option
              v-for="item in IndicatorData"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="文件" prop="fileName">
          <el-select size="mini" v-model="editForm.sourceId" placeholder="请选择文件">
            <el-option
              v-for="item in fileData"
              :key="item.id"
              :label="item.fileName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>


      </el-form>
<!--      <div class="container">-->
<!--        <el-row :gutter="18">-->
<!--          <el-col :span="12">-->
<!--            <h3>文件列表</h3>-->
<!--            <el-table-->
<!--              :data="fileData"-->
<!--              style="width: 100%"-->
<!--              @selection-change="handleSelectChangeA"-->
<!--              ref="singleSelectTable"-->
<!--            >-->
<!--              <el-table-column-->
<!--                type="selection"-->
<!--                width="55"-->
<!--              ></el-table-column>-->
<!--              <el-table-column-->
<!--                prop="id"-->
<!--                v-if="false"-->
<!--                label="序号"-->
<!--              ></el-table-column>-->
<!--              <el-table-column-->
<!--                prop="fileName"-->
<!--                label="文件名称"-->
<!--              ></el-table-column>-->
<!--            </el-table>-->
<!--            <Pagination v-bind:child-msg="pageparm2" @callFather="callFather1"></Pagination>-->
<!--          </el-col>-->

<!--          <el-col :span="12">-->
<!--            <h3>指标列表(多选)</h3>-->
<!--            <el-table-->
<!--              :data="IndicatorData"-->
<!--              style="width: 100%"-->
<!--              @selection-change="handleSelectChangeB"-->
<!--              ref="multipleSelectTable"-->
<!--            >-->
<!--              <el-table-column-->
<!--                type="selection"-->
<!--                width="55"-->
<!--              ></el-table-column>-->
<!--              <el-table-column-->
<!--                prop="id"-->
<!--                v-if="false"-->
<!--                label="序号"-->
<!--              ></el-table-column>-->
<!--              <el-table-column-->
<!--                prop="name"-->
<!--                label="指标名称"-->
<!--              ></el-table-column>-->
<!--            </el-table>-->
<!--            <Pagination v-bind:child-msg="pageparm1" @callFather="callFather2"></Pagination>-->
<!--          </el-col>-->
<!--        </el-row>-->
<!--      </div>-->
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click='closeDialog("edit")'>取消</el-button>
        <el-button size="small" @click='check'>测试</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">保存</el-button>
      </div>
    </el-dialog>





  </div>
</template>

<script>
  // 导入请求方法

  import Pagination from '../../components/Pagination'
  import axios from "axios";

  import UnStructDataMan from "../dataManage/UnStructDataMan.vue";
  export default {
    data() {
      return {

        tagData:[],
        fileData:[],
        isShow:false,
        isShow1:false,

        loading: false, //是显示加载
        title: '新增任务',
        editFormVisible: false, //控制编辑页面显示与隐藏

        showDataDetail:false, // 控制非结构化数据源信息展示页面

        fileName:[],
        indicatorIdArray:'',
        // 编辑与添加
        editForm: {
          id: '',
          sourceId: '',   // 非结构化文档id
          indicatorId:'' ,// 指标id
          status:'未执行',
          userId: localStorage.getItem('userId')
        },
        // 选择数据
        selectdata: [],
        // rules表单验证
        rules: {
          taskName: [
            { required: true, message: '请输入任务名称', trigger: 'blur' }
          ],



        },

        // 请求数据参数
        formInline: {
          page: 1,
          limit: 10,
          fileName: '',
          isLock: ''
        },



        //任务数据
        taskData: [],
        // 非结构化数据源信息
        UnStructSourceData:[],

        // 条件数据
        IndicatorData:[],
        ResponseData:[],
        SourceDataId:[],
        // 分页参数
        Data:[],
        // 指标集合分页
        pageparm: {
          currentPage: 1,
          pageSize: 10,
          total: 10
        },


        dialogVisible: false,
        checkedData: [],
        checkedListA: [], // 多选的数据
        checkedListB: null // 单选的数据
      }
    },
    // 注册组件
    components: {
      Pagination,
      UnStructDataMan
    },

    /**
     * 数据发生改变
     */
    watch: {},

    /**
     * 创建完毕
     */
    created() {
      this.data1 = localStorage.getItem('userId')
      this.getSearchPage()

    },

    /**
     * 里面的方法只有被调用才会执行
     */
    methods: {
      handleSelectChangeA(selection) {
        // 单选逻辑，确保只选中一个
        if (selection.length > 1) {
          this.$refs.singleSelectTable.clearSelection();
          this.$message.warning('文件只允许单选！');
        } else {
          this.handleSelect(selection, 'A');
        }
      },
      handleSelectChangeB(selection) {
        // 多选逻辑，无需处理
        this.handleSelect(selection, 'B');
      },
      handleSelect(selection, listType) {
        // 处理选中的项
        console.log(`List ${listType} selected:`, selection);
        // 你可以在这里执行你需要的逻辑，比如发送请求等
      },

      // changeTag(){
      //   this.fileData = []
      //   //
      //   for(var i = 0; i < this.UnStructSourceData.length; i++){
      //     if(this.UnStructSourceData[i].tagId == this.editForm.tagId){
      //       this.fileData[this.fileData.length] = {id:this.UnStructSourceData[i].id, fileName:this.UnStructSourceData[i].fileName}
      //     }
      //   }
      // },
      // 获取任务数据
      getSearchPage(formInline) {
        this.logining = true
        let url = this.$global.javaUrl +"/indicatorConfig/list"
        let config = {
          headers:
            {
              //'Content-Type':'application/json',
              'user' : localStorage.getItem('userId')
            },
          params: {
            fileName:this.formInline.fileName,
            pageNum: this.formInline.page,
            pageSize:this.formInline.limit,

          }
        }
        axios.get(url, config).then(response=>{
          this.taskData = response.data.data.rows
          this.pageparm.total = response.data.data.records
          console.log(JSON.stringify(this.taskData, null, 2)); // 以格式化的方式显示对象内容
        })

        this.getUnStructSourceData()
        this.getIndicator()



      },
      check(){
        this.editForm.indicatorId = this.indicatorIdArray.join(',')
        console.log(JSON.stringify(this.editForm, null, 2)); // 以格式化的方式显示对象内容
      },


      getUnStructSourceData(){
        this.logining = true
        this.fileData = []
        let url = this.$global.javaUrl+"/dataSource/listUnStruct"
        let config = {
          headers:
            {
              //'Content-Type':'application/json',
              'user' : localStorage.getItem('userId')
            },
          params: {
            pageNum: this.formInline.page,
            pageSize:1000,
            tagId:0

          }
        }

        axios.get(url, config).then(response=>{
          this.UnStructSourceData = response.data.data.rows

           // 以格式化的方式显示对象内容
          for(var i = 0; i < this.UnStructSourceData.length; i++){
              this.fileData[this.fileData.length] = {id:this.UnStructSourceData[i].id, fileName:this.UnStructSourceData[i].fileName}
          }


          console.log(JSON.stringify(this.fileData , null, 2));
        })


      },
      // 获取执行条件
      getIndicator() {
        this.logining = true
        let url = this.$global.javaUrl+"/indicator/list"
        let config = {
          headers:
            {
              //'Content-Type':'application/json',
              'user' : localStorage.getItem('userId')
            },
          params:
            {
              name:this.formInline.fileName,
              pageNum: this.formInline.page,
              pageSize:this.formInline.limit,

            }
        }
        axios.get(url, config).then(response=>{
          this.IndicatorData = response.data.data.rows
          console.log("e"+this.pageparm.total)
          console.log(JSON.stringify(this.IndicatorData, null, 2)); // 以格式化的方式显示对象内容
        })



      },

      lossFocus(){
        if(this.editForm.indicatorId!=undefined
          &&this.editForm.sourceId!=undefined){//条件满足，获取table信息
          console.log("满足条件---")
          let url = this.$global.javaUrl+"/target/list"
          let config = {
            headers: {
              'Content-Type': 'multipart/form-data',
              'user' : localStorage.getItem('userId')
            }
          }
          axios.get(url,{
            headers:
              {
                'user' : localStorage.getItem('userId')
              },
            params:
              {
                name:this.editForm.taskName,
                pageNum: '1',
                pageSize:'10',
              }
          }).then(res=>{
            if(res.status == 200){
              console.log("返回信息")
              console.log(res.data.data.rows)
              var list_data = res.data.data.rows
              this.Tableoptions = []
              for(var i = 0; i < list_data.length; i++){
                this.Tableoptions[i] = {label:list_data[i],value:list_data[i]}
              }
            }else{

            }
          }).catch(err=>{
            this.$message.error('链接信息不正确')
          })

        }
      },

      // 立即执行
      actionNow(id,status) {
        this.logining = true
        if(status == "已执行"){
          this.$message({
            type: 'warning',
            message: "任务已执行，请勿重复操作！"
          })
        }else{
          let url = this.$global.javaUrl+"/indicatorConfig/actionNow"
          let config = {
            headers:
              {
                //'Content-Type':'application/json',
                'user' : localStorage.getItem('userId')
              },
            params:{'id':id}

          }

          const loading = this.$loading({
            lock: true,
            text: '任务执行中',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          });

          axios.get(url,config).then(response=>{
            this.ResponseData = response.data.data.rows

            if (response.data.code == 200) {
              this.$message({
                type: 'success',
                message: "任务执行完成，可查看结果！"
              })
              this.getSearchPage()
            } else {
              this.$message({
                type: 'info',
                message: response.msg
              })
            }

          }).finally(() => {
            loading.close()
          })

        }


      },
      // 分页插件事件
      callFather(parm) {
        this.formInline.page = parm.currentPage
        this.formInline.limit = parm.pageSize
          this.getSearchPage()
      },

      //显示编辑界面
      handleEdit: function(index, row) {
        this.editFormVisible = true
        if (row != undefined && row != 'undefined') {
          this.title = '修改任务'
          this.editForm.id = row.id

        } else {
          this.isShow = false
          this.isShow1 = false
          this.title = '新增任务'
          this.editForm.id = ''
          this.editForm.indicatorId = ''
          this.editForm.sourceId = ''
        }
      },
      // 编辑、添加提交方法
      submitForm(editData) {

        this.editForm.indicatorId = this.indicatorIdArray.join(',')
        let config = {
          headers:
            {
              'Content-Type':'application/json',
              'user' : localStorage.getItem('userId')
            }
        }
        this.$refs[editData].validate(valid => {
          if (valid) {
            console.log("fffffffffff",this.editForm)
            // 请求方法
            axios
              .post(this.$global.javaUrl+"/indicatorConfig/saveOrUpdate",this.editForm,config)
              .then(res => {
                this.editFormVisible = false
                this.loading = false
                if (res.data.data != null) {
                  this.$message({
                    type: 'success',
                    message: "数据保存成功！"
                  })
                  this.getSearchPage()
                } else {
                  this.$message({
                    type: 'info',
                    message: res.msg
                  })
                }
              })
              .catch(err => {
                this.editFormVisible = false
                this.loading = false
                this.$message.error('保存失败，请稍后再试！')
              })
          } else {
            return false
          }
        })

      },


      // 选择复选框事件
      selectChange(val) {
        this.selectdata = val
      },
      // 关闭编辑、增加弹出框
      closeDialog(dialog) {
        if (dialog == 'edit') {
          this.isShow1 = false
          this.isShow = false
          this.showDataDetail = false
          this.showStructDataDetail = false
          this.editFormVisible = false
          this.editForm = ''
          this.indicatorIdArray = ''

        } else if (dialog == 'perm') {
          this.dataAccessshow = false
        } else if (dialog == 'unit') {
          this.unitAccessshow = false
        }
      },


      // 删除任务
      deleteTask(id) {
        let config = {
          headers:
            {
              'Content-Type':'application/json',
              'user' : localStorage.getItem('userId')
            },
          params:{'id':id}

        }

        this.$confirm('确定要删除吗?', '信息', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            // 删除
            axios.get(this.$global.javaUrl+"/indicatorConfig/delete",config)
              .then(res => {
                if (res.data.data !=null) {
                  this.$message({
                    type: 'success',
                    message: '数据已删除!'
                  })
                  this.getSearchPage()
                } else {
                  this.$message({
                    type: 'info',
                    message: "1111"
                  })
                }
              })
              .catch(err => {
                this.loading = false
                this.$message.error('数据删除失败，请稍后再试！')
              })
            this.getSearchPage()
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除！'
            })
          })
      },
      // 查看执行结果
      getRes(id,status,fileName){
        if(status=='已执行'){
          this.$router.push({path:'indicatorResult',query:{id:id,fileName:fileName}});
        }
        else{
          this.$message({
            type: 'info',
            message: '任务未执行，无法查看!'
          })
        }
      },
      //重置
      reset() {
        this.formInline.page = 1
        this.formInline.taskName = ""
        this.getSearchPage(this.formInline)
      },
      openDialog() {
        this.dialogVisible = true;
        this.$refs.page.clearSelection();

      },
      onConfirm() {
        const s = this.$refs.page.getSelection();

        if(s==0){

          this.$message({
            type: 'warning',
            message: "未选择数据,无法保存"
          })

        }else{
          this.dialogVisible = false;
          this.checkedData = s;

          console.log("11",this.checkedData)
        }

      },
      cancel(){
        this.editForm.sourceType = ''
        this.dialogVisible = false
        this.isShow = false
      }
    }
  }
</script>

<style scoped>
  .user-search {
    margin-top: 20px;
  }
  .userRole {
    width: 100%;
  }
</style>

